#include <stdio.h>
#include <stdlib.h>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        if (head -> next == NULL) {
            return NULL;
        }
        ListNode *pre = head;
        ListNode *cur = head;
        for (int i = 0; i < n; i ++) {
            cur = cur -> next;
        }
        if (cur == NULL) {
            return head -> next;
        }
        while (cur -> next != NULL) {
            cur = cur -> next;
            pre = pre -> next;
        }
        pre -> next = pre -> next -> next;
        return head;
    }
};